#!/bin/sh
uname && if [ "$(uname)" = "Linux" ]; then
    # check if ps exists and can be run on the current target with our selected options, flags is not supported in all ps implementations
    ps -axww -o pid=,flags=,comm=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,args= >/dev/null 2>/dev/null
    ret=$?
    if [ $ret -eq 0 ]; then
        ps -axww -o pid=,flags=,comm=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,args=
    else
        # if ps doesn't support the options we are after try and read from /proc/$pid
        # Try to mimic the output of ps -axww -o pid=,flags=,comm=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,args= from /proc/$pid/ data
        echo "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        for pid in `cd /proc && ls -d [0-9]*`
            do {
                if [ -e /proc/$pid/stat ]
                then
                    # read values from stat file, directory name matches the pid of each process running
                    # item 9 kernel flag words of the process, item 2 is the process file name, man proc has more details
                    flags=`awk '{print $9}' /proc/$pid/stat`;
                    command=`awk '{print $2}' /proc/$pid/stat | tr -d '()'`

                    # read the process command line used to start the process, 
                    # take the second item for display as argument passed 
                    args=`xargs -0 < /proc/$pid/cmdline | awk '{print $2}'`;

                    # write values in a format with similar spacing to procps-ng/procps ps
                    # pid 5 spaces with padding right align, flags 1 char, 
                    # command uses space size of the input of 50 a's -1 from heading left align, args 27 left align
                    printf "%5s %.1s %-49s %-27s \n\r" $pid $flags $command $args
                fi
            };
        done
    fi

elif [ "$(uname)" = "Darwin" ]; then ps -axww -o pid=,flags=,comm=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,args= -c; fi
